package chapter01.part2;

import chapter01.part1.CountWordByLength;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * 创建流的各种方式
 * 
 * @author insight 
 * @since 2021/7/19
 */
public class CreateStream {
    public static final Pattern COMPILE = Pattern.compile("\\PL+");
    private static final List<String> WORD_LIST = CountWordByLength.getWordList();

    public static void main(String[] args) {
        Stream<String> words = Stream.of("");

        Stream<String> song = Stream.of("gently", "down", "the", "stream");

        Stream<String> silence = Stream.empty();

        List<String> echo =
                Stream.generate(() -> "Echo").limit(10).collect(Collectors.toList());
        System.out.println(echo);

        Stream<Double> random = Stream.generate(Math::random);

        Stream<BigInteger> integers = Stream.iterate(BigInteger.ONE,
                n -> n.add(BigInteger.ONE));

        BigInteger bigInteger = new BigInteger("1000000");
        Stream<BigInteger> limitIntegers = Stream.iterate(BigInteger.ONE,
                n -> n.compareTo(bigInteger) < 0,
                n -> n.add(BigInteger.ONE));

        Stream<String> wordsAnotherWay = COMPILE.splitAsStream(
                "");

        try(Stream<String> lines = Files.lines(Paths.get(""))) {
            List<String> list = lines.collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }

        Iterable<Path> iterable =
                FileSystems.getDefault().getRootDirectories();
        Stream<Path> rootDirectories =
                StreamSupport.stream(iterable.spliterator(), false);

        Iterator<Path> iterator = Paths.get("").iterator();
        Stream<Path> stream =
                StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator,
                Spliterator.ORDERED), false);
    }

    public static Supplier<Stream<String>> getWordsStream() {
        return WORD_LIST::stream;
    }
}